package com.example.localmusic;

import android.annotation.SuppressLint;
import android.content.Context;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.io.IOException;
import java.util.List;

public class MusicAdapter extends RecyclerView.Adapter<MusicAdapter.musicViewHolder> {

    Context context;
    List<musicPojo> date;

    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    OnItemClickListener listener;

    public MusicAdapter(Context context, List<musicPojo> date) {
        this.context = context;
        this.date = date;
    }

    public interface OnItemClickListener{
        public void onItemClickListener(View view, int position) throws IOException;
    }




    @NonNull
    @Override
    public musicViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_local_music, parent, false);
        musicViewHolder musicViewHolder = new musicViewHolder(view);
        return musicViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull musicViewHolder holder, @SuppressLint("RecyclerView") int position) {
        musicPojo musicPojo = date.get(position);
        holder.idTv.setText(musicPojo.getId());
        holder.songTv.setText(musicPojo.getSong());
        holder.singerTv.setText(musicPojo.getSinger());
        holder.albumTv.setText(musicPojo.getAlbum());
        holder.timeTv.setText(musicPojo.getTime());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    listener.onItemClickListener(v, position);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return date.size();
    }

    class musicViewHolder extends RecyclerView.ViewHolder{

        TextView idTv, songTv, singerTv, albumTv, timeTv;

        public musicViewHolder(@NonNull View itemView) {
            super(itemView);
            idTv = itemView.findViewById(R.id.music_item_num);
            songTv = itemView.findViewById(R.id.music_item_song);
            singerTv = itemView.findViewById(R.id.music_item_singer);
            albumTv = itemView.findViewById(R.id.music_item_album);
            timeTv = itemView.findViewById(R.id.music_item_time);

        }
    }
}
